package com.yonyou.cyxdms.sysAuth.service.utill;

import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * DateUtil
 *
 * @author LiJun
 * @Description
 * @date 2019/6/4 12:57
 */
public class DateUtil {

    public static final String FORMAT_DEFAULT = "yyyyMMddHHmmss";
    public static final String FORMAT_YYYYMMDDHH_HHMMSS = "yyyyMMdd HHmmss";
    public static final String FORMAT_YYYY_MM_DD_HHMMSS = "yyyy-MM-dd HH:mm:ss";
    public static final String FORMAT_YYYY_MM_DD_T_HHMMSS_SSSZ = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
    public static final String FORMAT_MMdd = "MMdd";
    public static final String FORMAT_yyyyMMdd = "yyyyMMdd";
    public static final String FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
    public static final String FORMAT_time = "HH:mm:ss";

    public static LocalDateTime date2LocalDateTime(Date date) {
        Instant instant = date.toInstant();//An instantaneous point on the time-line.(时间线上的一个瞬时点。)
        ZoneId zoneId = ZoneId.systemDefault();//A time-zone ID, such as {@code Europe/Paris}.(时区)
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return localDateTime;
    }

    public static LocalDate date2LocalDate(Date date) {
        Instant instant = date.toInstant();//An instantaneous point on the time-line.(时间线上的一个瞬时点。)
        ZoneId zoneId = ZoneId.systemDefault();//A time-zone ID, such as {@code Europe/Paris}.(时区)
        LocalDate localDate = instant.atZone(zoneId).toLocalDate();
        return localDate;
    }

    public static String localDate2Str(LocalDate localDate, String pattern) {
        pattern = StringUtils.isEmpty(pattern) ? FORMAT_YYYY_MM_DD_HHMMSS : pattern;
        //This class is immutable and thread-safe.@since 1.8
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return dateTimeFormatter.format(localDate);
    }

    public static String localDateTime2Str(LocalDateTime localDateTime, String pattern) {
        pattern = StringUtils.isEmpty(pattern) ? FORMAT_YYYY_MM_DD_HHMMSS : pattern;
        //This class is immutable and thread-safe.@since 1.8
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return dateTimeFormatter.format(localDateTime);
    }

    public static Date localDateTime2Date(LocalDateTime localDateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);//Combines this date-time with a time-zone to create a  ZonedDateTime.
        Date date = Date.from(zdt.toInstant());
        return date;
    }

    /**
     * 获得指定日期格式：如20180901
     */
    public static String getyyyymmdd() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(Calendar.getInstance().getTime());
    }
}
